// $(function(){
// 	if(sessionStorage.href==""||sessionStorage.href==undefined||sessionStorage.href==null){	
// 	}else{
// 		location.href=location.href+"?"+sessionStorage.href;
// 	}
// })

//新书页面和免费页面的切换
//var arr1=[];
//if(location.href.split("?")[1]=="new"){
//	$(".logo").text("新书")
//	arr1=["最新上架","最热新书","连载新书","完本新书"];
//	for(var i=0;i<$(".homepage_list").length;i++){
//		$(".homepage_list").eq(i).find("h2").find("a").html(arr1[i]);
//	}
//}else if(location.href.split("?")[1]=="free"){
//	$(".logo").text("免费")
//	arr1=["最新免费","最热免费","连载免费","完本免费"]
//	for(var i=0;i<$(".homepage_list").length;i++){
//		$(".homepage_list").eq(i).find("h2").find("a").html(arr1[i]);
//	}
//}
$("nav>li").on("click", function () {
    $(window).scrollTop(0);
    $("nav>li").removeClass("active").attr("check", "false");
    $(".homepage_list").css("display", "none");
    $(this).addClass("active").attr("check", "true");
    $("nav>li").find("a").css("display", "none");
    $(this).find("a").css("display", "block");
    $(".homepage_list").eq($(this).index()).css("display", "block");
    var index = $(this).index();
    var page = $(this).attr('data-page');
    var isFinish = 999;
    if (index == 2) {
        isFinish = 2;
    } else if (index == 3) {
        isFinish = 1;
    }
    var wordCount = 0;
    var needPay = 999;
    for (var i = 0; i < $("aside>div>ul>li").length; i++) {
        if(i == 0 || i == 1){
            if ($("aside>div>ul>li").eq(i).attr("check") == "true") {
                needPay = $("aside>div>ul>li").eq(i).attr("data-value");
            }
        }else{
            if ($("aside>div>ul>li").eq(i).attr("check") == "true") {
                wordCount = $("aside>div>ul>li").eq(i).attr("data-value");
            }
        }
    }
    
    var action;
    if(type == 'new'){
        action = 'newBook';
    }else if(type == 'free'){
        action = 'freeBook';
    }else if(type == 'chosen'){
        action = 'chosenBook';
    }
    var from = $(".return_back").attr("data-home");
    var url = "novel/novel/"+action+"?orderBy="+index+"&isFinish="+isFinish+"&wordCount="+wordCount+"&needPay="+needPay+"&from="+from;
    if(type == 'category'){
        action = 'category';
        url = "novel/novel/"+action+"?ctype="+ctype+"&orderBy="+index+"&isFinish="+isFinish+"&wordCount="+wordCount+"&needPay="+needPay+"&from="+from;
    }
    window.history.pushState({},0,'http://'+window.location.host+'/'+url);
    var cached = $(".homepage_list").eq(index).attr('data-cached');
    if (cached != 'true') {
        $(".homepage_list").eq(index).find("li").remove();
        $("nav>li").eq(index).attr('data-page',1);
        getNewBook(index, 1, isFinish, wordCount, needPay,ctype);
    }

})

//点击书籍进入对应的书籍详情
$(".homepage_list>li").on("click", function () {
    var nid = $(this).attr('data-id');
    location.href = "../novel/novelInfo?novelid="+nid;
})
//跳转搜索页面
$(".search_hot").on("click", function () {
    location.href = "../novel/search"
})
//返回上一页面
$(".return_back").on("click", function () {
    var from = $(this).attr("data-home");
    if(from != ''){
        if(from == 'home'){
            location.href="../novel/novelHomePage";
        }else if(from == 'category'){
            location.href="../novel/category";
        }
    }else{
        window.history.back(-1);
    }
})
$("aside>div").on("click", function (e) {
    var e = e || event;
    e.stopPropagation();
    return false;
})
//属性的选择
$("aside>div>ul").eq(0).find("li").on("click", function (e) {
    var e = e || event;
    e.stopPropagation();
    $("aside>div>ul").eq(0).find("li").removeClass("active");
    $("aside>div>ul").eq(0).find("li").addClass("check", "false")
    $(this).addClass("active");
    $(this).attr("check", "true");
    $("aside>div>ul").eq(0).find("li").find("img").css("display", "none")
    $(this).find("img").css("display", "block")
})
//字数的选择
$("aside>div>ul").eq(1).find("li").on("click", function (e) {
    var e = e || event;
    e.stopPropagation();
    $("aside>div>ul").eq(1).find("li").removeClass("active");
    $("aside>div>ul").eq(1).find("li").attr("check", "false");
    $(this).addClass("active");
    $(this).attr("check", "true");
    $("aside>div>ul").eq(1).find("li").find("img").css("display", "none");
    $(this).find("img").css("display", "block");
})
//重置所有选择
$("aside>div>p>span").eq(0).on("click", function (e) {
    var e = e || event;
    e.stopPropagation();
    $("aside>div>ul>li").removeClass("active").find("img").css("display", "none");
    $("aside>div>ul").eq(0).find("li").attr("check", "false");
    $("aside>div>ul").eq(1).find("li").attr("check", "false");
})
//筛选界面的显示隐藏
$(".homepage_list>h2>span").on("click", function () {
    $("aside").css("visibility", "visible").css("transition", "none").css("background", "rgba(0,0,0,0.62)");
    $("aside>div").css("transform", "translateX(0)");
    $("html").css("overflow", "hidden");
    $("aside").on("click", function (e) {
        var e = e || event;
        e.stopPropagation();
        $("aside").css("visibility", "hidden");
        $("aside>div").css("transform", "translateX(14.02rem)");
        $("html").css("overflow", "auto")
    })
})
//分类遮罩显示隐藏
$(".classifty_btn").on("click", function () {
    if ($(this).attr("check") == "false") {
        $(this).attr("check", "true");
        $(".classifty").css("display", "block");
        $(".classifty_nav>li").eq(0).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../novel/newBook";
        })
        $(".classifty_nav>li").eq(1).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../novel/rankBook";
        })
        $(".classifty_nav>li").eq(2).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../html/classifty.html";
        })
        $(".classifty_nav>li").eq(3).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../novel/freeBook";
        })
    } else {
        $(".classifty").css("display", "none");
        $(this).attr("check", "false")
    }
})
$(".classifty").on("click", function () {
    $(".classifty_btn").trigger("click")
})

//加载更多筛选书籍
$(window).on("scroll",function(){
    if($(this).scrollTop()>=$(document).height()-$(window).height() ){
        // 加载剩余书籍
        var index = 0;
        var page = 1;
        for (var i = 0; i < $("nav>li").length; i++) {
            if ($("nav>li").eq(i).attr("check") == "true") {
                index = i;
                page = $("nav>li").eq(i).attr('data-page');
            }
        }
        var isFinish = 999;
        if (index == 2) {
            isFinish = 2;
        } else if (index == 3) {
            isFinish = 1;
        }
        var wordCount = 0;
        for (var i = 0; i < $("aside>div>ul>li").length; i++) {
            if(i == 0 || i == 1){
                if ($("aside>div>ul>li").eq(i).attr("check") == "true") {
                    needPay = $("aside>div>ul>li").eq(i).attr("data-value");
                }
            }else{
                if ($("aside>div>ul>li").eq(i).attr("check") == "true") {
                    wordCount = $("aside>div>ul>li").eq(i).attr("data-value");
                }
            }
        }
        
        var needPay = 999;
        for (var i = 0; i < $("aside>div>ul>li").length; i++) {
            if ($("aside>div>ul>li").eq(i).attr("check") == "true") {
                needPay = $("aside>div>ul>li").eq(i).attr("data-value");
            }
        }
        var action;
        if(type == 'new'){
            action = 'newBook';
        }else if(type == 'free'){
            action = 'freeBook';
        }else if(type == 'chosen'){
            action = 'chosenBook';
        }
        var url = "novel/novel/"+action+"?orderBy="+index+"&isFinish="+isFinish+"&wordCount="+wordCount+"&needPay="+needPay;
        if(type == 'category'){
            action = 'category';
            url = "novel/novel/"+action+"?ctype="+ctype+"&orderBy="+index+"&isFinish="+isFinish+"&wordCount="+wordCount+"&needPay="+needPay;
        }
        window.history.pushState({},0,'http://'+window.location.host+'/'+url);
//        var cached = $(".homepage_list").eq(index).attr('data-cached');
//        if (cached != 'true') {
//            $(".homepage_list").eq(index).find("li").remove();
            getNewBook(index, page, isFinish, wordCount, needPay,ctype);
//        }
    }
})
//$(window).on("scroll", function () {
//    if ($(this).scrollTop() == $(".homepage_list").height() - $(window).height() + 110) {
//        
//    }
//})

function getNewBook(index, page, isFinish, wordCount, needPay,ctype) {
    var h = $(".hide").eq(index).val();
    if(h == 0){
        return false;
    }
    var url = "/novel/novel/";
    if (type == 'free') {
        url += "freeBook";
    } else if (type == 'new') {
        url += "newBook";
    } else if (type == 'chosen') {
        url += "chosenBook";
    }
    var data = {orderBy: index, page: page, isFinish: isFinish, wordCount: wordCount, needPay: needPay};
    if (type == 'category') {
        url += "category";
        data = {ctype: ctype, orderBy: index, page: page, isFinish: isFinish, wordCount: wordCount, needPay: needPay};
    }
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        async:false,
        data: data,
        success: function (data) {
            if (data.status == 0) {
                var list = data.data.list.list;
                var total = data.data.list.total;
                var pageNum = data.data.params.pageNum;
                if(pageNum <= page){
                    $(".hide").eq(index).val('0');
                }
                var str = '';
                for (i in list) {
                    str += '<li data-id="' + list[i]['novel_id'] + '">' +
                                '<img src="' + list[i]['novel_cover'] + '">' +
                                '<div>' +
                                    '<h4>' + list[i]['novel_title'] + '</h4>' +
                                    '<h6>' + list[i]['novel_introduction'] + '</h6>' +
                                    '<p>' +
                                        '<span>作者 ' + list[i]['novel_author'] + '</span>' +
                                        '<a href="javascript:;">' + list[i]['novel_type'] + '</a>' +
                                        '<a href="javascript:;">' + list[i]['novel_category'] + '</a>' +
                                    '</p>' +
                                '</div>' +
                                '<a href="javascript:;"></a>' +
                            '</li>';
                }
                $(".homepage_list").eq(index).append(str);
                $(".homepage_list").eq(index).attr('data-cached', true);
                $("nav>li").eq(index).attr('data-page', data.data.params.page);
                //点击书籍进入对应的书籍详情
                $(".homepage_list>li").on("click", function () {
                    var nid = $(this).attr('data-id');
                    location.href = "../novel/novelInfo?novelid="+nid;
                })
            } else {
                $(".message_info_alert>span").html("加载失败");
                $(".message_info_alert").fadeIn(function () {
                    setTimeout(function () {
                        $(".message_info_alert").fadeOut()
                    }, 300)
                })
            }
        },
        error: function (e) {
            $(".message_info_alert>span").html("系统繁忙请稍后再试");
            $(".message_info_alert").fadeIn(function () {
                setTimeout(function () {
                    $(".message_info_alert").fadeOut()
                }, 300)
            })
        }
    });
}